﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace Jy123.Web.Mood
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class AddComment : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //int userID = 1;//暂时写死用户ID
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            int userID = int.Parse(strUserIdFromCookie);
            string strMoodID = context.Request["MoodID"];
            string strcommentcontent = HttpUtility.UrlDecode(context.Request["commentcontent"]);
            if (!string.IsNullOrEmpty(strMoodID)&&!string.IsNullOrEmpty(strcommentcontent.Trim()))
            {
                int addMoodCommentID = 0;
                Jy123.Model.User_MoodComment mMoodComment = new Jy123.Model.User_MoodComment();
                Jy123.BLL.User_MoodComment bllMoodComment = new Jy123.BLL.User_MoodComment();
                mMoodComment.FUserCommentator = userID;
                mMoodComment.FMood = Convert.ToInt32(strMoodID);
                mMoodComment.CommentContent = strcommentcontent;
                mMoodComment.PostTime = DateTime.Now;
                mMoodComment.PostIP = Common.Public.GetClientIP();
                mMoodComment.IsLock = false;
                addMoodCommentID = bllMoodComment.Add(mMoodComment);
                string strDel = "<img src=\"/images/del_blog.gif\" alt=\"删除\" onclick=\"javascript:myAjax('/Mood/MoodCommentDel.ashx','CommentID="+addMoodCommentID+"');\" />";
                if (addMoodCommentID > 0)
                {
                    //加入动态
                    Jy123.Model.User_Main mUserForState = new Jy123.Model.User_Main();
                    Jy123.Model.User_Main mUserFrd = new Jy123.Model.User_Main();
                    Jy123.BLL.User_Main bllUserForState = new Jy123.BLL.User_Main();

                    mUserForState = bllUserForState.GetModel(userID);
                    mUserFrd = bllUserForState.GetModel(mMoodComment.FUserCommentator);
                    Jy123.Model.User_Mood mMoodState = new Jy123.BLL.User_Mood().GetModel(mMoodComment.FMood);
                    Jy123.Model.User_MoodComment mMoodComm = new Jy123.BLL.User_MoodComment().GetModel(addMoodCommentID);
                    new Jy123.BLL.Public.State().CreateCommentMoodState(mUserForState,mUserFrd,mMoodState,mMoodComm);
                    //加入动态
                    string strHtml = context.Cache["user_moodcomment"].ToString();
                    strHtml = strHtml.Replace("$imgdel$",strDel);
                    strHtml = strHtml.Replace("$PostTime$",DateTime.Now.ToString());
                    strHtml = strHtml.Replace("$UserID$",userID.ToString());
                    strHtml = strHtml.Replace("$UserName$",new BLL.User_Main().GetModel(userID).UserName);
                    strHtml = strHtml.Replace("$CommentContent$",strcommentcontent);
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(strHtml);
                }
            }            
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
